package active

import (
	"fmt"
	"time"

	. "mlgmd.com/hearsay/base"
	"mlgmd.com/hearsay/entities"
	"mlgmd.com/hearsay/logic/push"
	"xorm.io/xorm"
)

func Active(DB xorm.Interface, uid int, typ string, id int) (err error) {
	defer Error(&err)
	now := time.Now()
	year, month, day := now.Date()
	today := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
	if !Must2(DB.Table(&entities.Activity{}).Where("user_id=? and created_time>?", uid, today.Unix()).Exist()) {
		Must2(DB.Insert(&entities.Activity{
			UserId:      uid,
			CreatedTime: time.Now().Unix(),
			TargetType:  typ,
			TargetId:    id,
			Coin:        1,
		}))
		Must2(DB.ID(uid).Incr("coin", 1).Incr("weight", 1).Update(&entities.User{}))
		_ = push.Send(uid, uid, fmt.Sprintf("今日参与互动，奖励1铜板"), "", 0, true)
	}
	return
}
